package akjvm.tools.java.classpath;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipFile;

/**
 * Created by VictoryWang on 2016/6/22.
 */
public class ZipEntry extends Entry {
    private File zipFile;

    public ZipEntry(String path) throws IOException {
        zipFile = new File(path);
    }

    @Override
    public byte[] readClass(String className) throws IOException {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends java.util.zip.ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {

            java.util.zip.ZipEntry entry = entries.nextElement();
            if (className.equals(entry.getName())) {
                return IOUtils.toByteArray(new AutoCloseInputStream(zip.getInputStream(entry)));
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return zipFile.getAbsolutePath();
    }
}
